package com.example.startservice2;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    private Button button_start;
    private Button button_finish;
    private Button button_stop;
    Intent intent = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
    }

    private void initView() {
        button_start = (Button) findViewById(R.id.button_start);

        button_start.setOnClickListener(this);
        button_finish = (Button) findViewById(R.id.button_finish);
        button_finish.setOnClickListener(this);
        button_stop = (Button) findViewById(R.id.button_stop);
        button_stop.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.button_start:
                intent = new Intent(MainActivity.this, MyService.class);
                startService(intent);
                break;
            case R.id.button_finish:
                finish();
                break;
            case R.id.button_stop:
                if (intent!=null){
                    stopService(intent);
                }
                break;
        }
    }
}
